package com.eric.ztea.controller;

import java.sql.Timestamp;
import java.util.List;

import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.eric.ztea.dao.BillRepository;
import com.eric.ztea.domain.Bill;

@RestController
@RequestMapping("/api/bill")
public class BillController {

	@Autowired
	BillRepository billRepository;

	@GET
	@RequestMapping(value = "/findById")
	public Bill findById(@RequestParam(value = "id", required = true) Long id) {
		return billRepository.findById(id);
	}
	
	@GET
	@RequestMapping(value = "/findByUserId")
	public List<Bill> findByUserId(@RequestParam(value = "userid", required = true) Long userid) {
		return billRepository.findByUserid(userid);
	}
	
	@GET
	@RequestMapping(value = "/findByEqId")
	public List<Bill> findByEqId(@RequestParam(value = "eqid", required = true) Long eqid) {
		return billRepository.findByEqid(eqid);
	}
	
	@POST
	@RequestMapping(value = "/save")
	public Bill save(@RequestParam(value = "id", required = false) Long id,
			@RequestParam(value = "userid", required = true) Long userid,
			@RequestParam(value = "eqid", required = true) Long eqid,
			@RequestParam(value = "startTime", required = true) Timestamp startTime,
			@RequestParam(value = "endTime", required = false) Timestamp endTime,
			@RequestParam(value = "cost", required = false) String cost,
			@RequestParam(value = "status", required = false) Long status) {
		return billRepository.save(new Bill(id,userid, eqid, startTime, endTime, cost, status));
	}
	
	@DELETE
	@RequestMapping(value = "/delete")
	public void delete(@RequestParam(value = "id", required = true) Long id)
	{
		billRepository.delete(id);
	}
}
